package com.sebastian.studies.ch5;

import java.util.Scanner;

public class Excercise_5_1_and_5_2_and_5_4 {

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);
		int integer;
		System.out.println("Enter an integer which you want to display in reverse order: ");
		integer = input.nextInt();

		System.out.println("The original integer is: " + integer);
		System.out.println("The reversed integer is: " + reverseInteger(integer));
		reverseIntegerTwo(integer);
		System.out.println("The sum is: " + summingIntegersDigits(integer));

		System.out.println("======EXPERIMENT1======");
		System.out.println("Before invoking function integer is: " + integer);
		int after = reverseInteger(integer);
		System.out.println("After invoking it and assigning to the variable after is: " 
			+ after + " but variable integer is still: " + integer);

		System.out.println("======EXPERIMENT2======");
		System.out.println("Additional proof");
		int x = 1;
		System.out.println("before: " + x);
		increment(x);
		System.out.println("after: " + x);
		
		input.close();
	}
	
	public static void increment(int n) {
		n++;
		System.out.println("inside: " + n);
	}

	public static int reverseInteger(int integer) {
		int reversedNum = 0;
		while (integer != 0) {
			reversedNum = reversedNum * 10 + integer%10;
			integer = integer/10;
		}
		return reversedNum;
	}

	public static void reverseIntegerTwo(int integer) {
		int reversedNum = 0;
		while (integer != 0) {
			reversedNum = reversedNum * 10 + integer%10;
			integer = integer/10;
		}
		System.out.println("The reversed integer using void function is: " + reversedNum);
	}

	public static int summingIntegersDigits(int integer) {

		int sum = 0;
		int revNubr = 0;
		while (integer != 0) {
			revNubr = integer%10;
			integer = integer/10;
			sum += revNubr;
		}
		
		return sum;
	}
}
